//
//  RegistViewController.m
//  Index
//
//  Created by Apple－12 on 15/10/10.
//  Copyright (c) 2015年 Apple－12. All rights reserved.
//

#import "RegistViewController.h"
#import "MRegistService.h"

@interface RegistViewController ()

@end

@implementation RegistViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    idText.delegate = self;
    passwordText.delegate = self;
    testPasswordText.delegate = self;
    
    self.finishButton.enabled = NO;
    if (userDefaults == NULL) {
        userDefaults = [[NSUserDefaults alloc]init];
    }
    
    [self.view sizeToFit];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(IBAction)testPassword:(id)sender{

    
     UIAlertView *alert = [[UIAlertView alloc]init];
//    if (![self checkInput:idText.text] && ![self checkInput1:idText.text]) {
//        
//        alert = [[UIAlertView alloc]initWithTitle:@"警告" message:@"请输入手机号码或邮箱！" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
//        [alert show];
//    }
//    else if (passwordText.text.length < 6 || passwordText.text.length > 12) {
//        alert = [[UIAlertView alloc]initWithTitle:@"警告" message:@"请输入6～12位数字！" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
//        [alert show];
//
//    }    else
    
    
    if (![testPasswordText.text isEqualToString:passwordText.text]) {
        alert = [[UIAlertView alloc]initWithTitle:@"警告" message:@"两次密码不相同，请重新输入！" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
    }
//    else if([self checkInput2:passwordText.text]){
//        
//        
//        
//        
//        alert = [[UIAlertView alloc]initWithTitle:@"警告" message:@"密码不能全数字！" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
//        [alert show];
//    }
    
    
    
    else{
   
        
        NSString *idtext = idText.text;
        NSString *pwtext = passwordText.text;
        
//        [userDefaults setObject:pwtext forKey:idtext];
        
        [userDefaults setObject:idtext forKey:@"username"];
        [userDefaults setObject:pwtext forKey:@"password"];
        
        
        
        
        
        
        [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(showAlert) userInfo:nil repeats:NO];
//        等待旋转图标
        activityIndicatorView = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
        activityIndicatorView .center = self.view.center;
        
        [self.view addSubview:activityIndicatorView];
        
        activityIndicatorView.color = [UIColor blueColor];
        [activityIndicatorView startAnimating];
        
        [activityIndicatorView setHidesWhenStopped:YES];
        
    }

}
//  判断注册账号是否为手机号码
//-(BOOL)checkInput:(NSString *)number{
//    NSString *phoneRegex = @"^1[34578]\\d{9}$";
//    
//    NSPredicate *phoneNumber = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
//    return [phoneNumber evaluateWithObject:number];
//   
//    
//}


//  判断注册账号是否为email
//-(BOOL)checkInput1:(NSString *)email{
//    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
//    NSPredicate *emailNumber = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",emailRegex];
//    return [emailNumber evaluateWithObject:email];
//}

//  判断密码是否为非全数字
//-(BOOL)checkInput2:(NSString *)passworld{
//    NSScanner *scan=[NSScanner scannerWithString:passworld];
//    int val;
//    return [scan scanInt:&val] && [scan isAtEnd];
//
//}


-(void)showAlert{

//    UIAlertView *alertView = [[UIAlertView alloc]init];
//
//    alertView = [[UIAlertView alloc]initWithTitle:@"恭喜" message:@"注册成功！" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
//    [alertView show];
    
    
    
    [[MRegistService sharedClient]getUserWithUsername:idText.text password:passwordText.text completion:^(NSString *result) {
        
        if (result) {
            NSLog(@"注册成功");
            UIAlertView *alertView = [[UIAlertView alloc]init];
            
            alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"注册成功！" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
            [alertView show];
            alertView.tag = 1;
        }
    } failure:^(NSString *error) {
        
        NSLog(@"注册失败");
        UIAlertView *alertView = [[UIAlertView alloc]init];
        
        alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"注册失败，请重新注册！" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alertView show];
        alertView.tag = 2;
        
    }];
    
    
    


    
    
    [activityIndicatorView stopAnimating];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    
    
    if (alertView.tag == 1) {
        if(buttonIndex == 0){
            [self dismissViewControllerAnimated:YES completion:nil];
        }

    }else if (alertView.tag == 2){
        if (buttonIndex == 0) {
            idText.text = NULL;
            passwordText.text = NULL;
            testPasswordText.text = NULL;
        }
    }
    
}


- (void)textFieldDidEndEditing:(UITextField *)textField{
    
    if ([textField.text isEqualToString:@""]) {
        self.finishButton.enabled = NO;
//        [self.finishButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        
    }
//        return YES;
}




-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    if (idText.tag == 100) {
        if (![idText.text isEqualToString:@""]&&![passwordText.text isEqualToString:@""]&&![testPasswordText.text isEqualToString:@""]) {
            self.finishButton.enabled = YES;
            [self.finishButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        }
    }
    return YES;
}



-(IBAction)backButtonTap:(id)sender{
    [self dismissViewControllerAnimated:YES completion:nil];
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
-(IBAction)tapBackground:(id)sender{
    [idText resignFirstResponder];
    [passwordText resignFirstResponder];
    [testPasswordText resignFirstResponder];

//    [self.view endEditing:YES];
}

@end
